package com.just.fun.java8;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * java8线程
 * 一个牛逼的多线程操作，CompletableFuture
 * CompletableFuture.supplyAsync(),定义要执行的异步任务
 * future.thenAccept(new Consumer<String>()) 重写accept方法定义回调函数
 */
public class J8ComFuture {
    public static void main(String[] args){
        //线程池貌似不能手动销毁
        ExecutorService executor= Executors.newFixedThreadPool(10);
        //要定义返回的类型
        CompletableFuture<String> future=CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("sleep begin");
                TimeUnit.SECONDS.sleep(5);
                System.out.println("sleep end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "message from future";
        },executor);
        future.thenAccept(o -> System.out.println("return msg to TC="+o));
        System.out.println("return RM msg to customer");
    }
}
